<?php
	namespace ampleharvest\task;

	require_once dirname(dirname(dirname(__FILE__))) . "/abstract_classes/abs_Task.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/Tables/cTABLE_Data.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/Tables/cTABLE_Metric.php";

	class count_DB_Docs extends \abs_Task
	{
		const C_STR_NAME = "count_DB_Docs";

		public function __construct($str_LockPath)
		{
			$this->set_LockPath($str_LockPath);
			$this->set_Name(self::C_STR_NAME);
			parent::__construct();
		}

		public function run($bool_Force = false)
		{
			$this->set_Force($bool_Force);
			if (!parent::run())
			{
				return false;
			}

			$m_arr_Status = array();

			$this->m_arr_Status[\cTABLE_Metric::C_STR_GROUP_INDEX_ERROR] = $this->collect_Count(\cTABLE_Data::C_INT_STATUS_INDEX_FAIL);
			$this->m_arr_Status[\cTABLE_Metric::C_STR_GROUP_INDEXED] = $this->collect_Count(\cTABLE_Data::C_INT_STATUS_INDEX_SUCCESS);
			$this->m_arr_Status[\cTABLE_Metric::C_STR_GROUP_INDEXING] = $this->collect_Count(\cTABLE_Data::C_INT_STATUS_INDEXING_DATA);
			$this->m_arr_Status[\cTABLE_Metric::C_STR_GROUP_NEW] = $this->collect_Count(\cTABLE_Data::C_INT_STATUS_NEW);
			$this->m_arr_Status[\cTABLE_Metric::C_STR_GROUP_PROCESS_ERROR] = $this->collect_Count(\cTABLE_Data::C_INT_STATUS_STAGE_1_FAIL);
			$this->m_arr_Status[\cTABLE_Metric::C_STR_GROUP_PROCESSED] = $this->collect_Count(\cTABLE_Data::C_INT_STATUS_STAGE_1_SUCCESS);
			$this->m_arr_Status[\cTABLE_Metric::C_STR_GROUP_PROCESSING] = $this->collect_Count(\cTABLE_Data::C_INT_STATUS_PROCESSING_DATA);

			foreach ($this->m_arr_Status as $key => $data)
			{
				$this->store_Hourly($data, $key);
				$this->store_Daily($data, $key);
				$this->store_Monthly($data, $key);
				$this->store_Yearly($data, $key);
			}
			return true;
		}

		private function store_Hourly($int_Count, $str_Group, $bool_Force = false)
		{
			$obj_StartTime = new \DateTime();
			$obj_StartTime->setTime($obj_StartTime->format("H"), 0, 0);

			$obj_EndTime = clone $obj_StartTime;
			$obj_EndTime->modify('+1 hours');

			$obj_TABLE_Metric = new \cTABLE_Metric();

			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Category,\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Group, $str_Group);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Subcategory, \cTABLE_Metric::C_STR_SUBCATEGORY_HOURLY);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_StartTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_GREATER);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_EndTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_LESS);

			$int_Return = $obj_TABLE_Metric->do_Search();

			if (($int_Return < 1) || ($bool_Force))
			{
				$obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
				$obj_TABLE_Metric->set_group($str_Group);
				$obj_TABLE_Metric->set_subcategory(\cTABLE_Metric::C_STR_SUBCATEGORY_HOURLY);
				$obj_TABLE_Metric->set_value($int_Count);

				$obj_TABLE_Metric->save_Data2DB();
			}
		}

		private function store_Daily($int_Count, $str_Group, $bool_Force = false)
		{
			$obj_StartTime = new \DateTime();
			$obj_StartTime->setTime(0, 0, 0);

			$obj_EndTime = clone $obj_StartTime;
			$obj_EndTime->modify('+1 days');

			$obj_TABLE_Metric = new \cTABLE_Metric();

			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Category,\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Group, $str_Group);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Subcategory, \cTABLE_Metric::C_STR_SUBCATEGORY_DAILY);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_StartTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_GREATER);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_EndTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_LESS);

			$int_Return = $obj_TABLE_Metric->do_Search();

			if (($int_Return < 1) || ($bool_Force))
			{
				$obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
				$obj_TABLE_Metric->set_group($str_Group);
				$obj_TABLE_Metric->set_subcategory(\cTABLE_Metric::C_STR_SUBCATEGORY_DAILY);
				$obj_TABLE_Metric->set_value($int_Count);

				$obj_TABLE_Metric->save_Data2DB();
			}

		}

		private function store_Monthly($int_Count, $str_Group, $bool_Force = false)
		{
			$obj_StartTime = new \DateTime();
			$obj_StartTime->setTime(0, 0, 0);
			$obj_StartTime->setDate($obj_StartTime->format("Y"), $obj_StartTime->format("m"), 1);

			$obj_EndTime = clone $obj_StartTime;
			$obj_EndTime->modify('+1 months');

			$obj_TABLE_Metric = new \cTABLE_Metric();

			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Category,\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Group, $str_Group);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Subcategory, \cTABLE_Metric::C_STR_SUBCATEGORY_MONTHLY);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_StartTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_GREATER);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_EndTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_LESS);

			$int_Return = $obj_TABLE_Metric->do_Search();

			if (($int_Return < 1) || ($bool_Force))
			{
				$obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
				$obj_TABLE_Metric->set_group($str_Group);
				$obj_TABLE_Metric->set_subcategory(\cTABLE_Metric::C_STR_SUBCATEGORY_MONTHLY);
				$obj_TABLE_Metric->set_value($int_Count);

				$obj_TABLE_Metric->save_Data2DB();
			}

		}
		private function store_Yearly($int_Count, $str_Group, $bool_Force = false)
		{
			$obj_StartTime = new \DateTime();
			$obj_StartTime->setTime(0, 0, 0);
			$obj_StartTime->setDate($obj_StartTime->format("Y"), 1, 1);

			$obj_EndTime = clone $obj_StartTime;
			$obj_EndTime->modify('+1 years');

			$obj_TABLE_Metric = new \cTABLE_Metric();

			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Category,\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Group, $str_Group);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Subcategory, \cTABLE_Metric::C_STR_SUBCATEGORY_YEARLY);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_StartTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_GREATER);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_EndTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_LESS);

			$int_Return = $obj_TABLE_Metric->do_Search();

			if (($int_Return < 1) || ($bool_Force))
			{
				$obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
				$obj_TABLE_Metric->set_group($str_Group);
				$obj_TABLE_Metric->set_subcategory(\cTABLE_Metric::C_STR_SUBCATEGORY_YEARLY);
				$obj_TABLE_Metric->set_value($int_Count);

				$obj_TABLE_Metric->save_Data2DB();
			}

		}
		private function collect_Count($int_Status)
		{
			$obj_TABLE_Data = new \cTABLE_Data();
			$obj_TABLE_Data->add_ColumnQuery($obj_TABLE_Data->m_int_COL_Status, $int_Status);

			return $obj_TABLE_Data->do_Count();
		}
	}
?>